﻿using Heroius.Extension.CSV;
using Microsoft.Win32;
using System.Collections.ObjectModel;
using System.IO;
using System.Text;
using System.Windows;

namespace FieldDataReader
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
        }

        /// <summary>
        /// 保存当前加载的数据
        /// </summary>
        public ObservableCollection<DataPoint> Data { get; set; } = [];

        private void Open_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new()
            {
                Filter = "*.csv|*.csv|*.ExplosionTest|*.ExplosionTest"
            };
            if (dialog.ShowDialog(this) == true)
            {
                using StreamReader stream = new(dialog.OpenFile(), Encoding.ASCII);
                try //防止非目标数据格式导致的异常。
                {
                    Data.Clear(); //清空数据池
                    //跳过非数据的行，数据格式的解释见本项目readme文档。
                    for (int i = 0; i < 10; i++)
                    {
                        stream.ReadLine();
                    }
                    while (!stream.EndOfStream)
                    {
                        var line = stream.ReadLine();
                        if (!string.IsNullOrWhiteSpace(line))
                        {
                            var parts = CsvHelper.ReadLine(line);
                            Data.Add(new()
                            {
                                Time = Convert.ToDouble(parts[3]), //时间。虽然文件中每个通道都保存了各自的时间数据，但实际上是一样的冗余设计。
                                Ch1_Pressure = Convert.ToDouble(parts[4]), //ch1
                                Ch1_Slope = Convert.ToDouble(parts[5]), //ch1
                                Ch2_Pressure = Convert.ToDouble(parts[7]), //ch2
                                Ch2_Slope = Convert.ToDouble(parts[8]) //ch2
                            });

                        }
                    }
                }
                catch
                {
                    MessageBox.Show("读取出错，格式不兼容");
                }
            }
        }

        private void Export_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog dialog = new()
            {
                Filter = "*.csv|*.csv"
            };
            if (dialog.ShowDialog() == true)
            {
                using StreamWriter writer = new(dialog.OpenFile(), Encoding.ASCII);
                foreach (var point in Data)
                {
                    writer.WriteLine(CsvHelper.WriteLine([
                        point.Time.ToString(),
                        point.Ch1_Pressure.ToString(),
                        point.Ch1_Slope.ToString(),
                        point.Ch2_Pressure.ToString(),
                        point.Ch2_Slope.ToString()
                        ]));
                }
            }
        }
    }
}